<template> 
    <el-card class="form-container" shadow="never">
        <el-form :model="pmsRepertory" :rules="rules" ref="PmsRepertoryFrom" label-width="150px">


                   <!-- 
                        <el-form-item
                            label="盘点ID"
                            prop="id">
                        <el-input v-model="pmsRepertory.id" style="width: 370px;"/>
                    </el-form-item>
                   -->


                    <el-form-item
                            label="商品分类-大"
                            prop="categoryParent">
                            <el-select  @change="productCatePChange" v-model="pmsRepertory.categoryParent" placeholder="请选择">
                                <el-option
                                v-for="item in productCateP"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                </el-option>
                            </el-select>
                    </el-form-item>


                    <el-form-item
                            label="商品分类-小"
                            prop="category">
                             <el-select @change="productCateCChange" v-model="pmsRepertory.category" placeholder="请选择">
                                <el-option
                                v-for="item1 in productCateC"
                                :key="item1.id"
                                :label="item1.name"
                                :value="item1.id">
                                </el-option>
                            </el-select>
                    </el-form-item>


                    <el-form-item
                            label="商品数量"
                            prop="num">
                            <el-input-number  v-enter-number 
                                v-model="pmsRepertory.num" >
                            </el-input-number>
                    </el-form-item>

                    <el-form-item
                            label="损耗"
                            prop="loss">
                            <el-input-number :precision="2"
                                v-model="pmsRepertory.loss" >
                            </el-input-number>
                    </el-form-item>
                    <el-form-item
                            label="商品总金额"
                            prop="totalAmount">
                            <el-input-number   
                                v-model="pmsRepertory.totalAmount" >
                            </el-input-number>
                    </el-form-item>

                   <!--
                         <el-form-item
                            label="盘点人"
                            prop="operBy">
                        <el-input v-model="pmsRepertory.operBy" style="width: 370px;"/>
                    </el-form-item>
                    -->


                    <el-form-item
                            label="盘点日期"
                            prop="operTime">
                               <el-date-picker
                                v-model="pmsRepertory.operTime"
                                value-format="yyyy-MM-dd"
                                type="date"
                                placeholder="选择日期">
                                </el-date-picker>
                    </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="onSubmit('PmsRepertoryFrom')">提交</el-button>
                <el-button v-if="!isEdit" @click="resetForm('PmsRepertoryFrom')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
    import {createPmsRepertory, getPmsRepertory, updatePmsRepertory} from '@/api/prm/pmsRepertory'
    import {fetchAllList} from '@/api/productCate';
    import SingleUpload from '@/components/Upload/singleUpload'
    import {fetchListAll,fetchList} from '@/api/productCate'
    const defaultPmsRepertory = {
        name: ''
    };
    export default {
        name: 'PmsRepertoryDetail',
        components: {SingleUpload},
        props: {
            isEdit: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
            productCateP:[],
            productCateC:[],
            pmsRepertory:
            Object.assign({},
        defaultPmsRepertory),
            rules: {
                name: [
                    {required: true, message: '请输入品牌名称', trigger: 'blur'},
                    {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
                ],
                    logo
            :
                [
                    {required: true, message: '请输入品牌logo', trigger: 'blur'}
                ],
                    sort
            :
                [
                    {type: 'number', message: '排序必须为数字'}
                ],
            }
        }
        },
        created() {
            if (this.isEdit) {
                getPmsRepertory(this.$route.query.id).then(response => {
                    this.pmsRepertory = response.data;
                    this.getProductCateChildren(this.pmsRepertory.categoryParent);
            })
                ;
            } else {
                this.pmsRepertory = Object.assign({},
            defaultPmsRepertory)
                ;
            }
            this.getProductCate();
           
        },
        methods: {
            productCatePChange(v){
                console.log(v);
                for(let item of this.productCateP){
                    if(item.id==v){
                        this.pmsRepertory.parentName = item.name;
                        break;
                    }
                }
                this.getProductCateChildren(v);
            },
            productCateCChange(v){
                this.$forceUpdate();
                for(let item of this.productCateC){
                    if(item.id==v){
                        this.pmsRepertory.categoryName = item.name;
                        break;
                    }
                }
            },
            getProductCate(){
               fetchAllList(0).then(response => {
                    if(response.code == 200)
                    {
                        this.productCateP = [];
                        if(response.data){
                            for(let item of response.data){
                                this.productCateP.push({
                                    id:item.id,
                                    name:item.name
                                })
                            }
                        }
                    }
                    else
                        {
                            this.$message({
                                message: response.msg,
                                type: 'error',
                                duration: 1000
                            });
                        }

                });
            },
            getProductCateChildren(c){
                fetchAllList(c).then(response => {
                    if(response.code == 200)
                    {
                        this.productCateC = [];
                        if(response.data){
                            for(let item of response.data){
                                this.productCateC.push({
                                    id:item.id,
                                    name:item.name
                                })
                            }
                            //this.pmsRepertory.category=null;
                        }
                    }
                    else
                        {
                            this.$message({
                                message: response.msg,
                                type: 'error',
                                duration: 1000
                            });
                        }

                });
            },
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if(valid) {
                        this.$confirm('是否提交数据', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            if(this.isEdit
                    )
                        {
                            updatePmsRepertory(this.$route.query.id, this.pmsRepertory).then(response => {
                                if(response.code == 200)
                            {
                                this.$refs[formName].resetFields();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    else
                        {
                            createPmsRepertory(this.pmsRepertory).then(response => {
                                if(response.code == 200
                        )
                            {
                                this.$refs[formName].resetFields();
                                this.pmsRepertory = Object.assign({},
                            defaultPmsRepertory)
                                ;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success',
                                    duration: 1000
                                });
                                this.$router.back();
                            }
                        else
                            {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    duration: 1000
                                });
                            }

                        })
                            ;
                        }
                    })
                        ;

                    } else {
                        this.$message({
                            message: '验证失败',
                            type: 'error',
                            duration: 1000
                        });
                return false;
            }
            })
                ;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.pmsRepertory = Object.assign({},
            defaultPmsRepertory)
                ;
            }
        }
    }
</script>
<style>
</style>


